package com.uniknow.app.manager;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.CallLog;

import com.uniknow.app.AppConfig;
import com.uniknow.app.bin.TBCallLog;
import com.uniknow.app.util.StringUtils;

public class CallLogManager extends BaseManager<TBCallLog> {

	public CallLogManager(Context context, Class<TBCallLog> clazz) {
		super(context, clazz);
	}
	
	/**
	 * 从系统通话记录中获取指定时间后的通话记录
	 * @param mContext
	 * @param startDate
	 *            "不包括毫秒级"
	 * @return
	 */
	public ArrayList<TBCallLog> GetCalogList(int startDate) {
		// select * from anyTable where var='const'
		// 那么anyTable就是uri，*就是projection，selection是“var=?"，selectionArgs写成这样：new
		// String[]{'const'}
		String startime = "";// 1970年1月1日0点作为"unix纪元"的原点
		if (startDate == 0) {
			startime = "0";// 查找全部通话记录
			System.out.println("开始时间为0");
		} else {
			startime = startDate + "000";// 补全毫秒级
			System.out.println("开始时间为startime=" + startime);
		}
		// startime = Long.toString(new Date().getTime());
		// System.out.println("当前时间："+Long.toString(new Date().getTime()));

		ArrayList<TBCallLog> CalogList = new ArrayList<TBCallLog>();
		ContentResolver cr = mContext.getContentResolver();
		final Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI,// uri
				new String[] { CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME,
						CallLog.Calls.TYPE, CallLog.Calls.DATE,
						CallLog.Calls.DURATION },// String[] projection
				CallLog.Calls.DATE + ">?",// String selection
				new String[] { startime },// String[] selectionArgs
				CallLog.Calls.DATE+" asc");// 排序字段
		if (cursor != null) {
			while (cursor.moveToNext()) {
				// System.out.println(cursor.getString(1)+":"+cursor.getInt(4)+":"+cursor.getLong(3));
				TBCallLog calog = new TBCallLog();
				calog.setTel(cursor.getString(0));
				calog.setName(cursor.getString(1));
				calog.setType(cursor.getInt(2));
				calog.setDate(StringUtils.FormatEpoch(cursor.getString(3)));
				calog.setRuntime(cursor.getInt(4));
				CalogList.add(calog);
			}
			cursor.close();
		}
		//System.out.println("CalogList = " + CalogList.size());
		return CalogList;

	}

	/**
	 * 插入通话记录到数据库
	 * @param CalogList
	 */
	public void InsertCalog(ArrayList<TBCallLog> CalogList) {
		SQLiteDatabase db = dataBaseHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			for (TBCallLog calog : CalogList) {
				db.execSQL(
						"INSERT INTO " + AppConfig.TB_PHONE_Calllog	+ "(name,date,tel,type,runtime) VALUES(?,?,?,?,?)",
						new Object[] {calog.getName(),calog.getDate(),calog.getTel(),calog.getType(),calog.getRuntime()
					});
			}
			db.setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.endTransaction();
		}

	}

	/**
	 * 获取Know数据库中calog表的通话记录最后一次通话记录时间
	 * @param db
	 * @return epcho
	 */
	public int GetCalogLastDate() {
		/*SQLiteDatabase db = dataBaseHelper.getWritableDatabase();
		int lastCalogDate = 0;
		String sql = "select date from " + AppConfig.TB_PHONE_Calllog
				+ " limit (select count(1) from " + AppConfig.TB_PHONE_Calllog
				+ ") offset  (select count(1) from "
				+ AppConfig.TB_PHONE_Calllog + " )-1";
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor.moveToNext()) {
			lastCalogDate = cursor.getInt(0);
		}
		return lastCalogDate;
		*/
		TBCallLog clog = getFirst(false);
		return Integer.parseInt(clog.getDate());
	}

}
